package com.noahplat.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.user.mapper.SysTypeMapper;
import com.noahplat.user.pojo.SysType;
import com.noahplat.user.pojo.User;
import com.noahplat.user.service.SysTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 系统相关操作
 *
 * @author 李东
 * @version 1.0.0
 * @Date: 2021/3/1 10:23
 **/
@Service
public class SysTypeServiceImpl implements SysTypeService {


    @Autowired
    private SysTypeMapper sysTypeMapper;

    /**
     * 通过系统类型查询系统信息
     *
     * @param sysType
     * @return
     * @author 李东
     * @version 1.0.0
     * @Date: 2021/3/1 10:23
     */
    @Override
    public Result<SysType> getSysInfoByName(String sysType) {
        Example example = new Example(SysType.class);
        example.createCriteria().andEqualTo("sysType", sysType);
        SysType sys = sysTypeMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(sys)) {
            return new Result(true, StatusCode.SERVER_EXCEPTION, "服务器异常！");
        }
        return new Result<SysType>(true, StatusCode.OK, "查询成功！", sys);
    }

    /**
     * 通过系统Id修改系统信息
     *
     * @param sysType
     * @return
     * @author 李东
     * @version 1.0.0
     * @Date: 2021/3/1 10:56
     */
    @Override
    public Result updateSysInfo(SysType sysType) {
        //校验系统信息
        Example example = new Example(SysType.class);
        example.createCriteria().andEqualTo("id", sysType.getId());
        SysType sys = sysTypeMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(sys)) {
            return new Result(true, StatusCode.NOTFOUNDERROR, "无系统信息！");
        }
        //更新系统信息
        sysType.setCreateTime(new Date());
        int i = sysTypeMapper.updateByExample(sysType, example);
        if (i == 0) {
            return new Result(true, StatusCode.SERVER_EXCEPTION, "服务器异常！");
        }
        return new Result<SysType>(true, StatusCode.OK, "修改成功！");
    }

    /**
     * 添加系统信息
     * @param sysType
     * @return
     */
    @Override
    public Result addSysType(SysType sysType){
        Example example = new Example(SysType.class);
        example.createCriteria().andEqualTo("sysType",sysType.getSysType());
        SysType sysType1 = sysTypeMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(sysType1)){
            return new Result(false,StatusCode.ERROR,"系统名称重复");
        }
        int res = sysTypeMapper.insertSelective(sysType);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 分页获取系统信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getSysType(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<SysType> list = sysTypeMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<SysType> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 获取系统信息
     * @return
     */
    @Override
    public Result getAllSysType(){
        List<SysType> list = sysTypeMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

}
